package com.gca.airhockey;


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;


public class SettingsPage extends Activity implements SeekBar.OnSeekBarChangeListener, 
			RadioGroup.OnCheckedChangeListener, ToggleButton.OnCheckedChangeListener {

	//UI elements
	SeekBar mSeekBar;
    TextView mProgressText;
    RadioGroup mRadioGroupPlayers;
    RadioGroup mRadioGroupLevel;
    RadioGroup mRadioGroupVictory;
    ToggleButton mToggleButtonMusic;
    
    //To save settings preferences
    public static final String USER_SETTINGS = "UserSettingsFile";
    int noPlayers = 1;
    String level = "Easy";
    int noGoals = 5;
    int friction = 0;
    Boolean music = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		
		mSeekBar = (SeekBar)findViewById(R.id.seekBarFriction);
        mSeekBar.setOnSeekBarChangeListener(this);
        mProgressText = (TextView)findViewById(R.id.editTextFriction);
        
		mRadioGroupPlayers = (RadioGroup)findViewById(R.id.GroupPlayers);
		mRadioGroupLevel = (RadioGroup)findViewById(R.id.GroupLevel);
		mRadioGroupVictory = (RadioGroup)findViewById(R.id.GroupVictory);
		
		mRadioGroupPlayers.setOnCheckedChangeListener(this);
		mRadioGroupLevel.setOnCheckedChangeListener(this);
		mRadioGroupVictory.setOnCheckedChangeListener(this);
		
		mToggleButtonMusic = (ToggleButton)findViewById(R.id.toggleButtonMusic);
		mToggleButtonMusic.setOnCheckedChangeListener(this);
		
	    SharedPreferences settings = getSharedPreferences(USER_SETTINGS, 0);
	    noPlayers = settings.getInt("noPlayers", 1);
        level = settings.getString("level", "Easy");
        noGoals = settings.getInt("noGoals", 5);
        friction = settings.getInt("friction", 0);
        music = settings.getBoolean("music", false);

        //Set last saved preferences as current ones when page appears
        
        switch (noPlayers) {
	        case 1: mRadioGroupPlayers.check(0x7f060008);
	        	break;
	        case 2: mRadioGroupPlayers.check(0x7f060009);
	        	break;
	        case 3: mRadioGroupPlayers.check(0x7f06000a);
	        	break;
	        case 4: mRadioGroupPlayers.check(0x7f06000b);
	        	break;
	        default: mRadioGroupVictory.clearCheck();
        		break;
        }
        
        if (level.compareTo("Easy") == 0)
        {
        	mRadioGroupLevel.check(0x7f06000f);
        }
        else if (level.compareTo("Medium") == 0)
        {
        	mRadioGroupLevel.check(0x7f060010);
        }
        else if (level.compareTo("Hard") == 0)
        {
        	mRadioGroupLevel.check(0x7f060011);
        }
        else
        {
        	mRadioGroupLevel.clearCheck();
        }
        
        switch (noGoals) {
	        case 5: mRadioGroupVictory.check(0x7f060015);
	        	break;
	        case 7: mRadioGroupVictory.check(0x7f060016);
	        	break;
	        case 9: mRadioGroupVictory.check(0x7f060017);
	        	break;
	        default: mRadioGroupVictory.clearCheck();
	        	break;
    	}
        
        mSeekBar.setProgress(friction);
        mProgressText.setText( String.valueOf(friction));
        
        mToggleButtonMusic.setChecked(music);
    }
	
	public void onPause() {
		super.onStop();
		
        SharedPreferences settings = getSharedPreferences(USER_SETTINGS, 0);
	    SharedPreferences.Editor editor = settings.edit();
	    
	    editor.putInt("noPlayers", noPlayers);
	    editor.putString("level", level);
	    editor.putInt("noGoals", noGoals);
	    editor.putInt("friction", friction);
	    editor.putBoolean("music", music);

	    editor.commit();
	}
	
	//Progress pour friction
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        mProgressText.setText( String.valueOf(progress) );
        
        friction = progress;
    }
	
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    public void onStopTrackingTouch(SeekBar seekBar) {

    }
    
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		RadioButton button = (RadioButton) findViewById(group.getCheckedRadioButtonId());
		
		//GroupLevels
		if (group.getId() == 0x7f06000e)
		{			
			level = button.getText().toString();
		}
		//GroupPlayers
		else if (group.getId() == 0x7f060007)
		{
			noPlayers = Integer.decode(button.getText().toString());
		}
		//GroupVictory
		else if (group.getId() == 0x7f060014)
		{
			noGoals = Integer.decode(button.getText().toString());
		}
		
	}

	public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
		music = arg1;
	}
}

